package com.qf.purchase.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.purchase.po.Brand;
import com.qf.purchase.po.Firm;
import com.qf.purchase.service.FirmService;
import com.qf.purchase.utils.LayUIOperate;
import com.qf.purchase.utils.TableData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Firm")
public class FirmController {
    @Resource
    private FirmService firmService;

    @RequestMapping("/getAllFirmList")
    @ResponseBody
    public TableData getAllFirmList(HttpServletRequest request) {
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));

        String name = request.getParameter("name");
        String userPerson = request.getParameter("userPerson");
        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String status = request.getParameter("status");

        Map params = new HashMap();
        params.put("name", name);
        params.put("userPerson", userPerson);
        if("0".equals(province)){
            params.put("province", null);
        }else {
            params.put("province", province);
        }
        if("0".equals(city)){
            params.put("city", null);
        }else {
            params.put("city", city);
        }
        if("0".equals(status)){
            params.put("status", null);
        }else {
            params.put("status", status);
        }

        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<Firm> data = firmService.getAllFirmByPage(params);
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setCount(data.getTotal());//设置总条数
        tableData.setData(data.getList());//设置当前的数据
        return tableData;
    }

    @RequestMapping("/addFirm")
    @ResponseBody
    public LayUIOperate addFirm(@RequestBody Firm firm) {
        LayUIOperate operate=new LayUIOperate();
        boolean f= firmService.addFirm(firm);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("厂商添加成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("厂商添加失败");
        }
        return operate;
    }
    @RequestMapping("/updateFirm")
    @ResponseBody
    public LayUIOperate updateFirm(@RequestBody Firm firm) {
        LayUIOperate operate=new LayUIOperate();
        boolean f= firmService.updateFirm(firm);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("厂商修改成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("厂商修改失败");
        }
        return operate;
    }
    @RequestMapping("/updateFirmStatus")
    @ResponseBody
    public LayUIOperate updateFirmStatus(HttpServletRequest request) {
        LayUIOperate operate=new LayUIOperate();
        String id = request.getParameter("id");
        String status = request.getParameter("status");
        Firm firm = firmService.findFirmById(Integer.parseInt(id));
        firm.setStatus(status);
        boolean f= firmService.updateFirm(firm);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("操作成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("操作失败");
        }
        return operate;
    }

    @RequestMapping("/findFirmById")
    @ResponseBody
    public Firm findFirmById(HttpServletRequest request) {
        String id = request.getParameter("id");
        Firm firm = firmService.findFirmById(Integer.parseInt(id));
        return firm;
    }

    @RequestMapping("/getFirmList")
    @ResponseBody
    public String getBrandList(HttpServletRequest request){
        Map params = new HashMap();
        List<Firm> data= firmService.getAllFirmByPage(params).getList();
        String msg = JSON.toJSON(data).toString();
        return msg;

    }
}
